package com.lagou.wang.servlet.student;

import com.lagou.wang.pojo.Student;
import com.lagou.wang.service.student.StudentService;
import com.lagou.wang.service.student.StudentServiceImpl;
import com.lagou.wang.utils.DateFormatUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实现流程控制和页面跳转
 *
 * @author wanggege
 */
@WebServlet(name = "StudentServlet", urlPatterns = {"/jsp/servlet"})
public class StudentServlet extends HttpServlet {
    //创建StudentService对象
    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取客户端传过来的指定参数，并实现对应的操作
        String param = request.getParameter("param");
        System.out.println(param);
        if (param.equals("all")) {
            this.allStudent(request, response, "allStudent.jsp");
        } else if (param.equals("toAdd")) {
            response.sendRedirect("addStudent.jsp");
        } else if (param.equals("add")) {
            this.addStudent(request, response);
            this.allStudent(request, response, "allStudent.jsp");
        } else if (param.equals("del")) {
            this.deleteStudentById(request, response);
            this.allStudent(request, response, "allStudent.jsp");
        } else if (param.equals("toUpdate")) {
            response.sendRedirect("updateStudent.jsp");
            int id = Integer.parseInt(request.getParameter("id"));
            Student student = this.queryStudentById(request, response, id);
            request.getSession().setAttribute("student", student);
        } else if (param.equals("update")) {
            this.updateStudentById(request, response);
            this.allStudent(request, response, "allStudent.jsp");
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);

    }


    /**
     * @param request
     * @param response
     * @param url
     * @throws IOException
     */
    public void allStudent(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {

        List<Student> studentList = studentService.allStudent();
        request.getSession().setAttribute("studentList", studentList);
        response.sendRedirect(url);

    }

    public void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int id = 0;
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Date birthday = DateFormatUtil.dateFormat(request.getParameter("birthday"));
        String email = name + "@163.com";
        String remark = request.getParameter("remark");
        System.out.println("获取到的用户名为：" + remark);
        studentService.addStudent(new Student(id, name, sex, birthday, email, remark));
        Student student = queryStudentByName(request, response, name);
        int studentId = student.getId();
        email = name + studentId + "@163.com";

        studentService.updateStudentById(new Student(studentId,name,sex,birthday,email,remark));

    }

    public void deleteStudentById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        studentService.deleteStudentById(id);

    }

    public void updateStudentById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Date birthday = DateFormatUtil.dateFormat(request.getParameter("birthday"));
        String email = name+id+"@163.com";
        String remark = request.getParameter("remark");
        studentService.updateStudentById(new Student(id, name, sex, birthday, email, remark));
    }

    public Student queryStudentById(HttpServletRequest request, HttpServletResponse response, int id) throws ServletException, IOException {
        Student student = studentService.queryStudentById(id);
        return student;
    }

    public Student queryStudentByName(HttpServletRequest request, HttpServletResponse response, String name) throws ServletException, IOException {
        Student student = studentService.queryStudentByName(name);
        return student;
    }
}
